package com.lulin.mall.product.thread;
import java.util.concurrent.*;

/**
 * ComplatableFuture异步编排介绍
 *
 * @author lulin
 * @date 2022/12/1 10:34
 */
public class ComplatableFutureDome {
    //创建1个线程池对象
    private static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 50, 10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
    );

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main---开始了。。。");
        //获取complatableFuture对象
        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
            System.out.println("线程开始了。。。");
            int i = 100 / 50;
            System.out.println("线程结束了。。。");

        }, poolExecutor);
        System.out.println("main---结束了。。。");


        System.out.println("-------------------------------------------------");
        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync——线程开始了。。。");
            int i = 100 / 50;
            System.out.println("supplyAsync——线程结束了。。。");
            return i;
        }, poolExecutor);
        //可以处理异步任务之后的操作




        System.out.println("获取线程的返回结果：" + supplyAsync.get());

    }


}
